<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="方案名称" prop="schemeName">
        <el-input
          v-model="queryParams.schemeName"
          placeholder="请输入方案名称"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="所属方案" prop="executeNumber">
        <el-select v-model="queryParams.executeNumber" filterable placeholder="请输入所属方案" clearable size="small" style="width: 160px">
          <el-option
            v-for="scheme in schemeIdOptions"
            :key="scheme.schemeName"
            :label="scheme.schemeName"
            :value="scheme.executeNumber"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="执行编码" prop="executeNumber">
        <el-input
          v-model="queryParams.executeNumber"
          placeholder="请输入执行编码"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="执行方式" prop="executeType">
        <el-select v-model="queryParams.executeType" placeholder="请选择执行方式" clearable size="mini">
          <el-option
            v-for="dict in executeTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="执行状态" prop="executeStatus">
        <el-select v-model="queryParams.executeStatus" placeholder="请选择执行状态" clearable size="mini">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="调用时间" prop="logTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.logTime"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择调用时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['log:log:add']"-->
<!--        >新增-->
<!--        </el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="logList.length < 1"
          @click="handleClean()"
          v-hasPermi="['log:log:remove']"
        >清空
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['log:log:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="logList" ref="fileTable"  @row-click="selectThisRow" @selection-change="handleSelectionChange" :row-class-name="tabRowClassName">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="方案名称" align="center" prop="schemeName" show-overflow-tooltip/>
      <el-table-column label="执行编码" align="center" prop="executeNumber" show-overflow-tooltip/>
      <el-table-column label="执行方式" align="center" prop="executeType" :formatter="executeTypeFormat"/>
      <el-table-column label="作业数量" align="center" prop="workNum"/>
      <el-table-column label="执行状态" align="center" prop="executeStatus" :formatter="executeStatusFormat"/>
      <el-table-column label="日志时间" align="center" prop="logTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.logTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行人" align="center" prop="executeor"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetails(scope.row)"
            v-hasPermi="['log:log:edit']"
          >详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 方案详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-dialog
        width="50%"
        title="详情"
        :visible.sync="innerVisible"
        append-to-body>
        <span v-model="content" :title="content">{{content}}</span><br>
        <el-button v-clipboard:copy="content" v-clipboard:success="onCopy" v-clipboard:error="onError"
                   type="info" icon="el-icon-info">
          复制
        </el-button>
      </el-dialog>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="方案名称:" prop="schemeName">
          <el-row :gutter="24">
            <el-col :span="10">
              {{form.schemeName}}
            </el-col>
            <el-form-item label="执行编码:" prop="executeNumber">
              {{form.executeNumber}}
            </el-form-item>
          </el-row>
        </el-form-item>
        <el-form-item label="执行方式:" prop="executeType" :formatter="executeTypeFormat">
          <el-row :gutter="24">
            <el-col :span="10">
              <div v-if="form.executeType === 1">任务执行</div>
              <div v-else-if="form.executeType === 2">消息执行</div>
              <div v-else-if="form.executeType === 3">手工执行</div>
            </el-col>
            <el-form-item label="作业数量:" prop="workNum">
              {{form.workNum}}
            </el-form-item>
          </el-row>
        </el-form-item>
        <el-form-item label="作业策略:" prop="executeStrategy">
          {{form.executeStrategy}}
        </el-form-item>
        <el-form-item label="执行状态:">
          <el-row :gutter="24">
            <el-col :span="10">
          <div v-if="form.executeStatus === 0">成功</div>
          <div v-else-if="form.executeStatus === 1">失败</div>
            </el-col>
          <el-form-item label="异常作业:" prop="errorWork">
            {{form.errorWork}}
          </el-form-item>
          </el-row>
        </el-form-item>
        <el-form-item label="异常信息:" prop="errorMsg">
          <el-row :gutter="24">
            <el-col :span="24">
              {{form.errorMsg | ellipsis}}
            </el-col>
          </el-row>
          <span v-model="form.errorMsg" :title="form.errorMsg"></span>
          <el-button v-if="form.errorMsg != null" type="info" icon="el-icon-info" size="small" @click="openInnerVisible(form.errorMsg)">
            查看
          </el-button>
        </el-form-item>
        <el-row :gutter="24">
          <el-form-item label="执行人:" prop="executeor">
            <el-col :span="5">
              {{form.executeor}}
            </el-col>
            <el-col :span="15" offset="10">
              <el-form-item label="日志时间:" prop="logTime">
                {{parseTime(form.logTime)}}
              </el-form-item>
            </el-col>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listLog, getLog, delLog, addLog, updateLog ,cleanSchemeLog} from "@/api/log/schemeLog";
  import {getSchemes} from "@/api/datafactory/scheme";
  import {parseTime} from "@/utils/ruoyi";

  export default {
    name: "SchemeLog",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 方案执行日志表格数据
        logList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        innerVisible: false,
        //复制内容
        content: '',
        // 执行方式字典
        executeTypeOptions: [],
        // 执行状态字典
        statusOptions: [],
        // 所属方案
        schemeIdOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          schemeName: null,
          executeNumber: null,
          executeType: null,
          executeStatus: null,
          logTime: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    filters: {
      ellipsis(value) {
        if (!value) return "";
        if (value.length > 400) {
          return value.slice(0, 400) + "...";
        }
        return value;
      }
    },
    created() {
      this.getList();
      this.getDicts("execute_type").then(response => {
        this.executeTypeOptions = response.data;
      });
      this.getDicts("sys_common_status").then(response => {
        this.statusOptions = response.data;
      });
      const scheme = {"status": 1};
      getSchemes(scheme).then(response => {
        this.schemeIdOptions = response;
      });
    },
    methods: {
      /** 更改斑马纹 */
      tabRowClassName({row,rowIndex}){
        let index = rowIndex + 1;
        if(index %2 == 0){
          return 'two-color'
        }else {
          return "one-color"
        }
      },
      /** 查询方案执行日志列表 */
      getList() {
        this.loading = true;
        listLog(this.queryParams).then(response => {
          this.logList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 复制 */
      onCopy(e) { 　　 // 复制成功
        this.$message({
          message: '复制成功！',
          type: 'success'
        })
      },
      onError(e) {　　 // 复制失败
        this.$message({
          message: '复制失败！',
          type: 'error'
        })
      },
      // 执行方式字典翻译
      executeTypeFormat(row, column) {
        return this.selectDictLabel(this.executeTypeOptions, row.executeType);
      },
      executeStatusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.executeStatus)
      },
      // 取消按钮
      cancel() {
        this.open = false;
      },
      // 表单重置
      reset() {
        this.innerVisible = false;
        this.content = '';
        this.form = {
          id: null,
          schemeName: null,
          executeNumber: null,
          executeType: null,
          workNum: null,
          executeStrategy: null,
          executeStatus: 0,
          errorWork: null,
          errorMsg: null,
          logTime: null,
          executeor: null,
          beginTime: null,
          endTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      selectThisRow(row){
        this.$refs.fileTable.toggleRowSelection(row);
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加方案执行日志";
      },
      /** 清空按钮操作 */
      handleClean() {
        this.$confirm('是否确认清空所有API日志?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          return cleanSchemeLog(this.queryParams);
        }).then(() => {
          this.getList();
          this.msgSuccess("清空成功");
        })
      },
      /** 详情按钮操作 */
      handleDetails(row) {
        this.reset();
        const id = row.id || this.ids
        getLog(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改方案执行日志";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateLog(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addLog(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除方案执行日志编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delLog(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      openInnerVisible(text){
        this.innerVisible = true;
        this.content = text;
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('log/log/export', {
          ...this.queryParams
        }, `log_log.xlsx`)
      }
    }
  };
</script>
